﻿@using RooHui.Infrastructure.Entities.Entity;
@using RooHui.Tool;
@{
    ViewBag.Title = "Index";
    Layout = "~/Views/Shared/_Base.cshtml";
}
<div id="toptoolbar"></div> 
<div id="productgrid">
</div>
<script type="text/javascript">
    var productgrid;
    $(function () {
        $("#toptoolbar").ligerToolBar({ items: [{ text: '新增', click: grid_product_add, icon: "add" },
            { text: '修改', click: grid_product_edit, icon: "modify" },
            { text: '删除', click: grid_product_delete, icon: "delete" },
            { text: '刷新', click: grid_product_refresh, icon: "refresh" }
            ]
        });
        productgrid = $("#productgrid").ligerGrid({
            columns: [
                { display: '产品名称', name: 'ProductName', width: '15%', isSort: false },
                { display: '产品描述', name: 'Summary', width: '20%', isSort: false },
                { display: '收益信息', name: 'RateSummary', width: '20%', isSort: false },
                { display: '状态', name: 'Status', width: '6%', isSort: false,
                    render: function (item) {
                        if (parseInt(item.Status) == 0) return '禁用';
                        else if (parseInt(item.Status) == 1) return '启用';
                    }
                },
                { display: '创建人', name: 'CreateUser', width: '8%', isSort: false },
                { display: '创建时间', name: 'CreateTime', width: '15%', isSort: false },
                {
                    display: '操作', isAllowHide: false,
                    render: function (row) {
                        var html = '<a href="#" onclick="productEdit(\'' + row.ID + '\', -1)">详情</a>&nbsp;&nbsp;<a href="#" onclick="productEdit(\'' + row.ID + '\',1)">修改</a>&nbsp;&nbsp;<a href="#" onclick="changestate(\'' + row.ID + '\', -1)">删除</a>';
                        if (row.Status == 1) {
                            html = html + '&nbsp;&nbsp;<a href="#" onclick="changestate(\'' + row.ID + '\',0)">禁用</a>';
                        }
                        else if (row.Status == 0) {
                            html = html + '&nbsp;&nbsp;<a href="#" onclick="changestate(\'' + row.ID + '\',1)">启用</a>';
                        }
                        return html;
                    }
                }
                ],
            width: '100%', height: '100%', rownumbers: true,
            dataAction: 'server',
            url: "/Product/GetProductListByPage",
            pageSizeOptions: [20],
            pageSize: 20
        });
    });

    function search(page) {
        if (page == 1) {
            productgrid.setOptions(
             {
                 parms:
                [
                    { name: "page", value: 1 }
                ]
             });
        }
        else {
            productgrid.setOptions();
        }
        productgrid.loadData();
    }

    // 添加
    var addDialog;
    function grid_product_add() {
        location.href = decodeURIComponent("/Product/ProductAdd");
    }

    function save_Product() {
        addDialog.frame.save();
    }

    function save_result(result) {
        $.ligerDialog.success("保存成功。");
        addDialog.close();
        search(0);
    }

    // 修改
    var editDialog;
    function grid_product_edit() {
        if (productgrid.getSelecteds().length == 0) {
            $.ligerDialog.warn('选择要修改的产品记录！', '温馨提示');
            return;
        }
        var productID = productgrid.getSelected().ID;
        productEdit(productID, 1);
    }

    function productEdit(productid, edittype) {
        location.href = decodeURIComponent("/Product/ProductEdit?productId=" + productid + "&editType=" + edittype);
    }

    function grid_product_delete() {
        var productId = "";

        var selected = productgrid.getSelecteds();
        if (selected.length == 0) {
            $.ligerDialog.warn('至少选择一条产品记录进行删除！', '温馨提示');
            return;
        }

        for (var i = 0; i < selected.length; i++) {
            productId += selected[i].ID + ",";
        }
        productId = productId.substring(0, productId.length - 1);
        changestate(productId, -1);

    }

    // 改变产品状态
    function changestate(productid, destinatstatus) {
        var deststatus;
        if (destinatstatus == 1) {
            deststatus = "启用";
        }
        else if (destinatstatus == 0) {
            deststatus = "禁用";
        }
        else if (destinatstatus == -1) {
            deststatus = "删除";
        }

        $.ligerDialog.confirm('确定要' + deststatus + '选中的产品吗？', '温馨提示', function (result) {
            if (result) {
                $.ajax({
                    url: '/Product/ProductStatusChange',
                    data: "productId=" + productid + "&destinationStatus=" + destinatstatus,
                    type: "post",
                    success: function (result) {
                        if (result == "OK") {
                            $.ligerDialog.success(deststatus + "成功！");
                            grid_product_refresh();
                        } else {
                            $.ligerDialog.error(deststatus + '失败！', '温馨提示');
                        }
                    }
                });
            }
        });
    }

    // 刷新
    function grid_product_refresh() {
        productgrid.setOptions(
             {
                 parms:
                [
                    { name: "page", value: 1 }
                ]
             });
        productgrid.loadData();
    }
 
         
  
</script>